package com.gxuwz.medical.domain.payStandard;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import com.gxuwz.medical.database.DbUtil;

/**
 * 缴费标准
 * 
 * @author Administrator
 * 
 */
public class PayStandard {

	private String thisYear; // 当前年度
	private double account; // 需缴费金额
	private Date startTime; // 缴费开始时间
	private Date endTime; // 缴费终止时间
	private String registrar; // 操作员

	public String getThisYear() {
		return thisYear;
	}

	public void setThisYear(String thisYear) {
		this.thisYear = thisYear;
	}

	public double getAccount() {
		return account;
	}

	public void setAccount(double account) {
		this.account = account;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public String getRegistrar() {
		return registrar;
	}

	public void setRegistrar(String registrar) {
		this.registrar = registrar;
	}

	public PayStandard() {
		super();
	}

	public PayStandard(String thisYear, double account, Date startTime,
			Date endTime, String registrar) {
		super();
		this.thisYear = thisYear;
		this.account = account;
		this.startTime = startTime;
		this.endTime = endTime;
		this.registrar = registrar;
	}

	public PayStandard(String thisYear) throws SQLException {
		this.thisYear = thisYear;
		getByThisYear();
	}

	/**
	 * 获取指定年度的缴费标准
	 */
	private void getByThisYear() throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConn();
			pstmt = conn
					.prepareStatement("select * from t_payStandard where thisYear=? ");
			int index = 1;
			// 把页面传过来的treeID的值赋给占位符
			pstmt.setString(index, this.thisYear);
			// 执行查询操作
			rs = pstmt.executeQuery();
			if (rs.next()) {
				this.thisYear = rs.getString("thisYear");
				this.account = rs.getDouble("account");
				this.startTime = rs.getDate("startTime");
				this.endTime = rs.getDate("endTime");
				this.registrar = rs.getString("registrar");
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}
	}

	public void AddPayStandard() throws Exception {
		Connection conn = null;
		try {
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			addPayStandard(conn);
			conn.commit();
		} catch (Exception e) {
			if (conn != null) {
				conn.rollback();
			}
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	private void addPayStandard(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"insert into t_payStandard(thisYear, account, "
							+ "startTime, endTime, registrar)");
			sqlBuff.append(" values(?,?,?,?,?)");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			int index = 1;
			pstmt.setString(index++, this.thisYear);
			pstmt.setDouble(index++, this.account);
			pstmt.setDate(index++, new java.sql.Date(this.startTime.getTime()));
			pstmt.setDate(index++, new java.sql.Date(this.endTime.getTime()));
			pstmt.setString(index++, this.registrar);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}
	}

	public void UpdatePayStandard() throws Exception {
		Connection conn = null;
		try {
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			updatePayStandard(conn);
			conn.commit();
		} catch (Exception e) {
			if (conn != null) {
				conn.rollback();
			}
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	private void updatePayStandard(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			StringBuffer sql = new StringBuffer();
			sql.append("update t_payStandard set account=?, startTime=?, endTime=?, registrar=? ");
			sql.append(" where thisYear=? ");
			pstmt = conn.prepareStatement(sql.toString());
			int index = 1;
			pstmt.setDouble(index++, this.account);
			pstmt.setDate(index++, new java.sql.Date(this.startTime.getTime()));
			pstmt.setDate(index++, new java.sql.Date(this.endTime.getTime()));
			pstmt.setString(index++, this.registrar);
			pstmt.setString(index++, this.thisYear);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}
	}
}
